package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.repository.statistics.LoanHitLibraryResultRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import org.jooq.Record7;
import org.jooq.Result;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.stereotype.Repository;

import java.util.Date;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanHitLibraryResult.LOAN_HIT_LIBRARY_RESULT;

/**
 * @author Simon
 * @date 2019/06/20 13:58
 */
@Repository
public class LoanHitLibraryResultRepositoryImpl extends JOOQBaseRepository implements LoanHitLibraryResultRepository {
    public void deleteNewUserByDay() {
        dslContext.delete(LOAN_HIT_LIBRARY_RESULT).where(LOAN_HIT_LIBRARY_RESULT.IS_NEW_USER.eq((byte) 1)).execute();
    }
    public void deleteOldUserByWeeks(String startTime) {
        dslContext.delete(LOAN_HIT_LIBRARY_RESULT).where(LOAN_HIT_LIBRARY_RESULT.CREATE_TIME.lt(DSL.timestamp(startTime))).execute();
    }
    public Result<Record7<String, Integer, Byte, String, Integer, String, String>> findAllByDate(Date day) {
        return dslContext
                .select(LOAN_HIT_LIBRARY_RESULT.PHONE, LOAN_HIT_LIBRARY_RESULT.HIT_LIB_TYPE, LOAN_HIT_LIBRARY_RESULT.IS_NEW_USER, LOAN_HIT_LIBRARY_RESULT.PRODUCT_ID,
                        LOAN_HIT_LIBRARY_RESULT.CHANNEL, LOAN_HIT_LIBRARY_RESULT.PLATFORM, LOAN_HIT_LIBRARY_RESULT.PHONEID)
                .from(LOAN_HIT_LIBRARY_RESULT)
                .where(LOAN_HIT_LIBRARY_RESULT.CDATE.eq(new java.sql.Date(day.getTime()))).fetch();
    }
    protected TableImpl getTable() {
        return LOAN_HIT_LIBRARY_RESULT;
    }
}